package com.dawn.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dawn.seckilldemo.mapper.OrderMapper;
import com.dawn.seckilldemo.pojo.Order;
import com.dawn.seckilldemo.pojo.SeckillGoods;
import com.dawn.seckilldemo.pojo.SeckillOrder;
import com.dawn.seckilldemo.pojo.User;
import com.dawn.seckilldemo.service.OrderService;
import com.dawn.seckilldemo.service.SeckillGoodsService;
import com.dawn.seckilldemo.service.SeckillOrderService;
import com.dawn.seckilldemo.utils.MD5Util;
import com.dawn.seckilldemo.utils.UUIDUtil;
import com.dawn.seckilldemo.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dawnfades
 * @since 2021-09-18
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SeckillOrderService seckillOrderService;
    @Autowired
    private RedisTemplate redisTemplate;

    //秒杀商品，减少库存
    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goodsVo) {
        //查询后端的库存量进行减一
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>()
                .eq("goods_id", goodsVo.getId()));
        //下面这句和判断库存不是原子性操作，会出先问题
//        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
//        seckillGoodsService.updateById(seckillGoods);
        //进行更新操作同时，判断库存是否大于0
        boolean update = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("stock_count = stock_count-1")
                .eq("goods_id", goodsVo.getId()).gt("stock_count", 0));
        if(seckillGoods.getStockCount()<1){
            //库存为null
            redisTemplate.opsForValue().set("isStockEmpty:"+goodsVo.getId(),"0");
            return null;
        }
        //生成普通订单
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);
        //生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrderService.save(seckillOrder);
        //存入redis
        redisTemplate.opsForValue().set("order:"+user.getId()+":"+goodsVo.getId(),seckillOrder);
        return order;
    }

    @Override
    public Order finbyOrderId(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        return order;
    }

    //获取秒杀地址,存在redis中
    @Override
    public String createPath(User user, Long goodsId) {
        String str = MD5Util.md5(UUIDUtil.uuid() + "123456");
        redisTemplate.opsForValue().set("seckillPath:"+user.getId()+":"+goodsId,str,60, TimeUnit.SECONDS);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {
        if(user == null){
            return false;
        }
        //path判断
        String str = (String) redisTemplate.opsForValue().get("seckillPath:" + user.getId() + ":" + goodsId);
        return path.equals(str);
    }
}
